//
//  PPSelectTagViewController.m
//  pet
//
//  Created by 陈无忌 on 11/19/14.
//  Copyright (c) 2014 whalefin. All rights reserved.
//

#import "PPSelectTagViewController.h"
#import "PPAddTopicViewController.h"
#import "PPSystemConfiguraton.h"
#import "PPCameraAuthorization.h"
#import "ELCImagePickerController.h"
#import "ELCImagePickerHeader.h"

@interface PPSelectTagViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,ELCImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *dogButton;
@property (weak, nonatomic) IBOutlet UIButton *catButton;
@property (weak, nonatomic) IBOutlet UIButton *birdButton;
@property (weak, nonatomic) IBOutlet UIButton *smallPetButton;
@property (weak, nonatomic) IBOutlet UIButton *amphibiansButton;
@property (weak, nonatomic) IBOutlet UIButton *fishButton;
@property (weak, nonatomic) IBOutlet UIButton *bugButton;

@property (strong, nonatomic) UIImagePickerController *imagePicker;
//@property (strong, nonatomic) ELCImagePickerController *ELCImagePicker;

@property (strong, nonatomic) NSMutableArray *imageArray;
@property (weak, nonatomic) IBOutlet UIButton *resultButton;

@property (strong, nonatomic) NSString *tagString;

@property (weak, nonatomic) PPSystemConfiguraton *config;

@end

@implementation PPSelectTagViewController
- (PPSystemConfiguraton *)config
{
    if (!_config) {
        _config = [PPSystemConfiguraton sharedSystemConfiguration];
    }
    return _config;
}

- (UIImagePickerController *)imagePicker
{
    if (!_imagePicker) {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.allowsEditing = NO;
        _imagePicker.delegate = self;
    }
    return _imagePicker;
}

//- (ELCImagePickerController *) ELCImagePicker{
//    if (!_ELCImagePicker) {
//        _ELCImagePicker = [[ELCImagePickerController alloc] ];
//        _ELCImagePicker.allowsEditing = NO;
//        _ELCImagePicker.delegate = self;
//    }
//    return _imagePicker;
//}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self buttonSet:self.dogButton];
    [self buttonSet:self.catButton];
    [self buttonSet:self.birdButton];
    [self buttonSet:self.smallPetButton];
    [self buttonSet:self.amphibiansButton];
    [self buttonSet:self.fishButton];
    [self buttonSet:self.bugButton];
    
    // Do any additional setup after loading the view.
}

- (void)buttonSet:(UIButton *)sender{
    sender.layer.borderColor = [UIColor whiteColor].CGColor;
    [sender addTarget:self action:@selector(selectTag:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)selectTag:(UIButton *)sender
{
    [self.resultButton setTitle:sender.titleLabel.text forState:UIControlStateNormal];
    self.tagString = sender.titleLabel.text;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Custom IBAction
- (IBAction)takePhotoButton:(id)sender {
    if (self.tagString.length > 0) {
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            if (IS_OS_7_OR_LATER) {
                [PPCameraAuthorization getCameraAuthorization];
                if (!self.config.isCameraValid) {
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请在iPhone的“设置-隐私-相机”选项中，允许程序访问你的相机" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil];
                    [alert show];
                    return;
                }else{
                    [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
                }
            }else{
               [self.imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
            }
            
        }else{
            [self.imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            
        }
        [self presentViewController:self.imagePicker animated:YES completion:nil];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择一个标签" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        [alert show];
    }
}

- (IBAction)selectPhotoButton:(id)sender {
    if (self.tagString.length > 0) {
        ELCImagePickerController *elcImagePicker = [[ELCImagePickerController alloc] initImagePicker];
        elcImagePicker.imagePickerDelegate = self;
        elcImagePicker.maximumImagesCount = 9;
        elcImagePicker.onOrder = YES;
        [self.navigationController presentViewController:elcImagePicker animated:YES completion:nil];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择一个标签" delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
        [alert show];
    }
    
    
}

- (IBAction)backButton:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIImagePickControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    UIImage *myimage = [info valueForKey:UIImagePickerControllerOriginalImage];
    self.imageArray = [NSMutableArray array];
    [self.imageArray addObject:myimage];
    [self.imagePicker dismissViewControllerAnimated:YES completion:^{
        PPAddTopicViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPAddTopicViewController"];
        targetVC.tagString = self.tagString;
        targetVC.imageArray = self.imageArray;
        [self.navigationController pushViewController:targetVC animated:YES];
    }];
}

#pragma mark - ELCImagePickerControllerDelegate
- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info
{
    self.imageArray = [NSMutableArray array];
    for (NSDictionary *dict in info) {
        if ([dict objectForKey:UIImagePickerControllerMediaType] == ALAssetTypePhoto){
            if ([dict objectForKey:UIImagePickerControllerOriginalImage]){
                EYLargePhoto *photo = [[EYLargePhoto alloc] init];
                photo.thumb = [dict objectForKey:UIImagePickerControllerOriginalImage];
                UIImage* image=photo.thumb;
                [self.imageArray addObject:image];
            } else if ([dict objectForKey:UIImagePickerControllerEditedImage]){
                NSLog(@"UIImagePickerControllerReferenceURL = %@", dict);
            }
        }
    }
    [picker dismissViewControllerAnimated:YES completion:^{
        PPAddTopicViewController *targetVC = [self.storyboard instantiateViewControllerWithIdentifier:@"PPAddTopicViewController"];
        targetVC.tagString = self.tagString;
        targetVC.imageArray = self.imageArray;
        [self.navigationController pushViewController:targetVC animated:YES];
    }];
}


- (void)elcImagePickerControllerDidCancel:(ELCImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}



@end
